const ProjectRequest = require("../project/project_request.js");
const Url = require("../utils/url.js");

/** 
 * page
 * @para types[]
 * @para filters:[]
 * @para paras: {} 
 */
function page(startPage, types, filters, paras, callback){
  const URL = Url.newInstance("/product/page");
  URL.setPara('startPage', startPage)
  for (let type of types) {
    URL.addPara('type', type)
  }
  // for (let filter of filters){
  //   URL.addPara('filter', filter)
  // }
  if (null !== paras.categoryId && undefined !== paras.categoryId){
    URL.setPara('categoryId', paras.categoryId)
  }
  //
  ProjectRequest.postRequest(URL.get(), { filters: filters }, callback);
}

/** 
 * 产品分类列表
 */
function categoryList(rootCategoryName, callback){
  const URL = Url.newInstance("/product/category/list");
  URL.setPara('rootCategoryName', rootCategoryName)
  //
  ProjectRequest.getRequest(URL.get(), callback);
}

/** 
 * page
 * @para types[]
 * @para filters:[] 过滤属性
 * @para filtersPara:{} 过滤参数
 */
function list(types, filters, filtersPara, callback) {
  const URL = Url.newInstance("/product/list");
  for (let type of types) {
    URL.addPara('type', type)
  }
  // 过滤属性
  // for (let filter of filters) {
  //   URL.addPara('filter', filter)
  // }
  // 过滤参数
  if (filtersPara.name){
    URL.setPara('name', filtersPara.name)
  }
  //
  ProjectRequest.postRequest(URL.get(), { filters: filters }, callback);
}

/** 
 * pageByWine
 * @para types[]
 * @para filters:[]
 */
function pageByWineId(startPage, types, filters, wineId, callback) {
  const URL = Url.newInstance("/product/page");
  URL.setPara('startPage', startPage)
  wineId && URL.setPara('wineId', wineId)
  for (let type of types) {
    URL.addPara('type', type)
  }
  // for (let filter of filters) {
  //   URL.addPara('filter', filter)
  // }
  //
  ProjectRequest.postRequest(URL.get(), { filters: filters}, callback);
}

/**
 * pageByTown
 * @para types[]
 */
function pageByTown(startPage, types, townId, callback) {
  const URL = Url
    .newInstance("/product/page")
    .setPara('startPage', startPage);
  for (let type of types) {
    URL.addPara('type', type)
  }
  if (null != townId){
    URL.setPara('townId', townId)
  }
  //
  ProjectRequest.getRequest(URL.get(), callback);
}

function findById(id, callback) {
  const url = "/product/" + id
  ProjectRequest.getRequest(url, callback);
}

function showImg(id, callback) {
  const url = "/product/" + id + "/showImg";
  ProjectRequest.getRequest(url, callback);
}

function listProperties(categoryId, callback) {
  const URL = Url
    .newInstance("/product/listProperties");
  if (null !== categoryId){
    URL.setPara('categoryId', categoryId);
  }
  ProjectRequest.getRequest(URL.get(), callback);
}

function listSlideImg(id, callback) {
  const url = "/product/" + id + "listSlideImg"
  ProjectRequest.getRequest(url, callback);
}

/**
 * exports
 */
module.exports = {
  findById: findById,
  page, list, pageByWineId,
  pageByTown: pageByTown,
  categoryList,
  showImg: showImg,
  listProperties: listProperties,
  listSlideImg: listSlideImg
};